package com.SouthernWall_404.age_tool.common.Network.Packets.S2C.ClientHandler;

import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MimicBlockRespondS2CPacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.S2C.MusicBlockNBTS2CPacket;
import com.SouthernWall_404.age_tool.common.world.Entity.MimicBlockEntity;
import com.SouthernWall_404.age_tool.common.world.Entity.MusicBlockEntity;
import com.SouthernWall_404.age_tool.common.world.Entity.TriggerBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class MimicBlockRespondPacketHandler {

    public static void hanlder(MimicBlockRespondS2CPacket msg, Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> {
            Level level = Minecraft.getInstance().level;
            if (level == null) return;

            BlockEntity blockEntity = level.getBlockEntity(msg.blockPos);

            if (blockEntity instanceof TriggerBlockEntity triggerBlockEntity) {
                if (msg.blockPack != null) {
                    triggerBlockEntity.mimicPack = msg.blockPack;
                    triggerBlockEntity.loadBlock();

                    // 强制重新渲染
                    level.sendBlockUpdated(
                            msg.blockPos,
                            level.getBlockState(msg.blockPos),
                            level.getBlockState(msg.blockPos),
                            3
                    );

                    // 请求区块重新渲染
                    if (Minecraft.getInstance().levelRenderer != null) {
                        Minecraft.getInstance().levelRenderer.setSectionDirty(
                                msg.blockPos.getX() >> 4,
                                msg.blockPos.getY() >> 4,
                                msg.blockPos.getZ() >> 4
                        );
                    }
                }
            } else if (blockEntity instanceof MimicBlockEntity mimicBlockEntity) {
                if (msg.blockPack != null) {
                    mimicBlockEntity.mimicPack = msg.blockPack;
                    mimicBlockEntity.loadBlock();

                    // 强制重新渲染
                    level.sendBlockUpdated(
                            msg.blockPos,
                            level.getBlockState(msg.blockPos),
                            level.getBlockState(msg.blockPos),
                            3
                    );

                    // 请求区块重新渲染
                    if (Minecraft.getInstance().levelRenderer != null) {
                        Minecraft.getInstance().levelRenderer.setSectionDirty(
                                msg.blockPos.getX() >> 4,
                                msg.blockPos.getY() >> 4,
                                msg.blockPos.getZ() >> 4
                        );
                    }
                }
            }
        });
        ctx.get().setPacketHandled(true);
    }
}
